import requests as rq
import hashlib
import time
import re
import json
import prettytable as pt
date = int(time.time() * 1000)
def Music(Song_name):
    #对歌曲的签名认证进行获取（利用time.time()生成的时间戳和）
    def seach_hash(date, key_word):
        s = [
            "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt",
            "appid=1014",
            "bitrate=0",
            "callback=callback123",
            f"clienttime={date}",
            "clientver=1000",
            "dfid=11Rvlc0QKmws4QRB8Z1TVTPQ",
            "filter=10",
            "inputtype=0",
            "iscorrection=1",
            "isfuzzy=0",
            f"keyword={key_word}",
            "mid=cca461cce711e8847c05c03f52ce6150",
            "page=1",
            "pagesize=30",
            "platform=WebFilter",
            "privilege_filter=0",
            "srcappid=2919",
            "token=",
            "userid=0",
            "uuid=cca461cce711e8847c05c03f52ce6150",
            "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt"
        ]
        # 把列表合并成字符串
        string = ''.join(s)
        # 通过MD5加密
        MD5 = hashlib.md5()
        # 传入需要加密的内容
        MD5.update(string.encode('utf-8'))
        # 加密处理
        sigenature = MD5.hexdigest()
        print(sigenature)
        return sigenature
    
    #
    headers = {
        'Cookie': 'kg_mid=a010df3d3da612f1802ff97f05e56cdd; kg_dfid=4XRrfA0QKnCM1w1OSN1rh0B3; '
                  'kg_dfid_collect=d41d8cd98f00b204e9800998ecf8427e; Hm_lvt_aedee6983d4cfc62f509129360d6bb3d=1702106556; '
                  'KuGooRandom=66681702106662419; ACK_SERVER_10015=%7B%22list%22%3A%5B%5B%22bjlogin-user.kugou.com%22%5D'
                  '%5D%7D; ACK_SERVER_10016=%7B%22list%22%3A%5B%5B%22bjreg-user.kugou.com%22%5D%5D%7D; '
                  'ACK_SERVER_10017=%7B%22list%22%3A%5B%5B%22bjverifycode.service.kugou.com%22%5D%5D%7D; '
                  'kg_mid_temp=a010df3d3da612f1802ff97f05e56cdd; '
                  'ACK_SERVER_10028=%7B%22list%22%3A%5B%5B%22p1.fx.kgimg.com%22%5D%2C%5B%22p3fx.service.kugou.com%22%5D'
                  '%5D%7D; ACK_SERVER_10029=%7B%22list%22%3A%5B%5B%22p3.fx.kgimg.com%22%5D%2C%5B%22p3fx.service.kugou.com'
                  '%22%5D%5D%7D; ACK_SERVER_10035=%7B%22list%22%3A%5B%5B%22fxsong3.kugou.com%22%5D%2C%5B%22fxsong4.kugou'
                  '.com%22%5D%2C%5B%22fxsong.kugou.com%22%5D%2C%5B%22fxsong2.kugou.com%22%5D%5D%7D; '
                  'ACK_SERVER_10011=%7B%22list%22%3A%5B%5B%22fanxing.kugou.com%22%5D%2C%5B%22sparefx2.kugou.com%22%5D%5D'
                  '%7D; ACK_SERVER_10034=%7B%22list%22%3A%5B%5B%22image.fanxing.kugou.com%22%5D%2C%5B%22s3fx.kgimg.com%22'
                  '%5D%5D%7D; ACK_SERVER_10022=%7B%22list%22%3A%5B%5B%22fx2.service.kugou.com%22%5D%2C%5B%22fxservice4'
                  '.kugou.com%22%5D%2C%5B%22fx1.service.kugou.com%22%5D%2C%5B%22fxservice3.kugou.com%22%5D%5D%7D; '
                  'ACK_SERVER_10014=%7B%22list%22%3A%5B%5B%22service.fanxing.kugou.com%22%5D%2C%5B%22servicefx2.kugou.com'
                  '%22%5D%5D%7D; ACK_SERVER_10023=%7B%22list%22%3A%5B%5B%22service2.fanxing.kugou.com%22%5D%2C%5B'
                  '%22service4.fanxing.kugou.com%22%5D%2C%5B%22service1.fanxing.kugou.com%22%5D%2C%5B%22service3.fanxing'
                  '.kugou.com%22%5D%5D%7D; ACK_SERVER_10033=%7B%22list%22%3A%5B%5B%22s3.fx.kgimg.com%22%5D%2C%5B%22s3fx'
                  '.kgimg.com%22%5D%5D%7D; ACK_SERVER_10036=%7B%22list%22%3A%5B%5B%22gateway.kugou.com%22%5D%2C%5B'
                  '%22gatewayretry.kugou.com%22%5D%5D%7D; '
                  'ACK_SERVER_10030=%7B%22list%22%3A%5B%5B%22p3fx.kgimg.com%22%5D%2C%5B%22p3fx.service.kugou.com%22%5D%5D'
                  '%7D; ACK_SERVER_10020=%7B%22list%22%3A%5B%5B%22fx.service.kugou.com%22%5D%2C%5B%22fxservice1.kugou.com'
                  '%22%5D%2C%5B%22fxservice2.kugou.com%22%5D%5D%7D; Hm_lpvt_aedee6983d4cfc62f509129360d6bb3d=1702127938',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 '
                      'Safari/537.36'
    }
    
    # 搜索接口
    link = 'https://complexsearch.kugou.com/v2/search/song'
    date = int(time.time() * 1000)
    key_word = Song_name
    sign = seach_hash(date, key_word)
    # 请求参数
    link_data = {
        'callback': 'callback123',
        'srcappid': '2919',
        'clientver': '1000',
        'clienttime': date,
        'mid': 'cca461cce711e8847c05c03f52ce6150',
        'uuid': 'cca461cce711e8847c05c03f52ce6150',
        'dfid': '11Rvlc0QKmws4QRB8Z1TVTPQ',
        'keyword': key_word,
        'page': '1',
        'pagesize': '30',
        'bitrate': '0',
        'isfuzzy': '0',
        'inputtype': '0',
        'platform': 'WebFilter',
        'userid': '0',
        'iscorrection': '1',
        'privilege_filter': '0',
        'filter': '10',
        'token': '',
        'appid': '1014',
        'signature': sign,
    }
    # 发送请求
    search_data = rq.get(url=link, params=link_data, headers=headers).text
    # 提取数据
    info = re.findall('callback123\((.*)', search_data)[0].replace(')', '')
    # 把json字符串转出json字典
    search_json_data = json.loads(info)
    tb = pt.PrettyTable()
    tb.field_names = [
        '序号',
        '歌手',
        '歌名',
        '专辑',
        'ID',
    ]
    info_list = []
    num = 0
    for index in search_json_data['data']['lists']:
        AlbumName = index['AlbumName']  # 专辑
        EMixSongID = index['EMixSongID']  # ID
        SingerName = index['SingerName']  # 歌手
        SongName = index['SongName']  # 歌名
        dit = {
            '序号':num,
            '歌手': SingerName,
            '歌名': SongName,
            '专辑': AlbumName,
            'ID': EMixSongID,
        }
        tb.add_row([num, SingerName, SongName, AlbumName, EMixSongID])
        info_list.append(dit)
        num += 1
    print(tb)
    return info_list

def lod(Song_name,Num):
    page = Num
    music_id = Music(Song_name)[int(page)]['ID']
    
    
    def hash(date, music_id):
        s = [
            "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt",
            "appid=1014",
            f"clienttime={date}",  # 时间戳
            "clientver=20000",
            "dfid=4XRrfA0QKnCM1w1OSN1rh0B3",
            f"encode_album_audio_id={music_id}",  # 音频id
            "mid=a010df3d3da612f1802ff97f05e56cdd",
            "platid=4",
            "srcappid=2919",
            "token=",
            "userid=0",
            "uuid=a010df3d3da612f1802ff97f05e56cdd",
            "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt"
        ]
        # 把列表合并成字符串
        string = ''.join(s)
        # 通过MD5加密
        MD5 = hashlib.md5()
        # 传入需要加密的内容
        MD5.update(string.encode('utf-8'))
        # 加密处理
        sigenature = MD5.hexdigest()
        print(sigenature)
        return sigenature
    
    
    headers = {
        'Cookie': 'kg_mid=a010df3d3da612f1802ff97f05e56cdd; kg_dfid=4XRrfA0QKnCM1w1OSN1rh0B3; '
                  'kg_dfid_collect=d41d8cd98f00b204e9800998ecf8427e; '
                  'Hm_lvt_aedee6983d4cfc62f509129360d6bb3d=1702106556; KuGooRandom=66681702106662419; '
                  'ACK_SERVER_10015=%7B%22list%22%3A%5B%5B%22bjlogin-user.kugou.com%22%5D%5D%7D; '
                  'ACK_SERVER_10016=%7B%22list%22%3A%5B%5B%22bjreg-user.kugou.com%22%5D%5D%7D; '
                  'ACK_SERVER_10017=%7B%22list%22%3A%5B%5B%22bjverifycode.service.kugou.com%22%5D%5D%7D; '
                  'kg_mid_temp=a010df3d3da612f1802ff97f05e56cdd; '
                  'ACK_SERVER_10028=%7B%22list%22%3A%5B%5B%22p1.fx.kgimg.com%22%5D%2C%5B%22p3fx.service.kugou.com%22'
                  '%5D%5D%7D; ACK_SERVER_10029=%7B%22list%22%3A%5B%5B%22p3.fx.kgimg.com%22%5D%2C%5B%22p3fx.service'
                  '.kugou.com%22%5D%5D%7D; ACK_SERVER_10035=%7B%22list%22%3A%5B%5B%22fxsong3.kugou.com%22%5D%2C%5B'
                  '%22fxsong4.kugou.com%22%5D%2C%5B%22fxsong.kugou.com%22%5D%2C%5B%22fxsong2.kugou.com%22%5D%5D%7D; '
                  'ACK_SERVER_10011=%7B%22list%22%3A%5B%5B%22fanxing.kugou.com%22%5D%2C%5B%22sparefx2.kugou.com%22%5D'
                  '%5D%7D; ACK_SERVER_10034=%7B%22list%22%3A%5B%5B%22image.fanxing.kugou.com%22%5D%2C%5B%22s3fx.kgimg'
                  '.com%22%5D%5D%7D; ACK_SERVER_10022=%7B%22list%22%3A%5B%5B%22fx2.service.kugou.com%22%5D%2C%5B'
                  '%22fxservice4.kugou.com%22%5D%2C%5B%22fx1.service.kugou.com%22%5D%2C%5B%22fxservice3.kugou.com%22'
                  '%5D%5D%7D; ACK_SERVER_10014=%7B%22list%22%3A%5B%5B%22service.fanxing.kugou.com%22%5D%2C%5B'
                  '%22servicefx2.kugou.com%22%5D%5D%7D; '
                  'ACK_SERVER_10023=%7B%22list%22%3A%5B%5B%22service2.fanxing.kugou.com%22%5D%2C%5B%22service4'
                  '.fanxing.kugou.com%22%5D%2C%5B%22service1.fanxing.kugou.com%22%5D%2C%5B%22service3.fanxing.kugou'
                  '.com%22%5D%5D%7D; ACK_SERVER_10033=%7B%22list%22%3A%5B%5B%22s3.fx.kgimg.com%22%5D%2C%5B%22s3fx'
                  '.kgimg.com%22%5D%5D%7D; ACK_SERVER_10036=%7B%22list%22%3A%5B%5B%22gateway.kugou.com%22%5D%2C%5B'
                  '%22gatewayretry.kugou.com%22%5D%5D%7D; '
                  'ACK_SERVER_10030=%7B%22list%22%3A%5B%5B%22p3fx.kgimg.com%22%5D%2C%5B%22p3fx.service.kugou.com%22'
                  '%5D%5D%7D; ACK_SERVER_10020=%7B%22list%22%3A%5B%5B%22fx.service.kugou.com%22%5D%2C%5B%22fxservice1'
                  '.kugou.com%22%5D%2C%5B%22fxservice2.kugou.com%22%5D%5D%7D; '
                  'Hm_lpvt_aedee6983d4cfc62f509129360d6bb3d=1702127938',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36'
    }
    # 请求网址
    url = 'https://wwwapi.kugou.com/play/songinfo'
    sigenature = hash(date, music_id)
    # 请求参数
    data = {
        'srcappid': '2919',
        'clientver': '20000',
        'clienttime': date,
        'mid': 'a010df3d3da612f1802ff97f05e56cdd',
        'uuid': 'a010df3d3da612f1802ff97f05e56cdd',
        'dfid': '4XRrfA0QKnCM1w1OSN1rh0B3',
        'appid': '1014',
        'platid': '4',
        'encode_album_audio_id': music_id,
        'token': '',
        'userid': '0',
        'signature': sigenature,
    }
    # 发送请求
    response = rq.get(url=url, params=data, headers=headers)
    json_data = response.json()
    # 歌名
    audio_name = json_data['data']['audio_name']
    # 歌的链接
    play_url = json_data['data']['play_url']
    #保存数据
    content = rq.get(url=play_url, headers=headers).content
    with open('music\\' + audio_name + '.mp3', mode='wb') as f:
        f.write(content)
    print(audio_name,'下载完毕！！！')
